<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="../../../js/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
		<link rel="stylesheet" type="text/css" href="../../../css/reset.css" />
		<link rel="stylesheet" type="text/css" href="../../../css/footer.css" />
		<link rel="stylesheet" type="text/css" href="../../../css/nearby.css" />
		<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=cEywgU0hSEVd31BAlBWPYT331WtoNNdB"></script>
		<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script> 
		<script src="../../../js/jquery-2.2.3.min.js"></script>
		<script src="../../../js/flexible.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../../js/jquery.cookie.js" type="text/javascript"></script>
		<script src="../../../js/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
		<script src="../../../js/rhAdmin.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			body {
				background: #fdfdfd;
			}
			
			.present {
				color: #a3a3a3;
			}
			
			.item-title {
				padding: 0 0.085rem;
			}
			
			.address {
				margin-bottom: 0.1805rem;
			}
			
			.icon {
				margin-right: 0.2083rem;
			}
			/*修改底部字体颜色*/
			
			.foot:nth-child(2) {
				color: #e83023;
			}
			
			.itemHref {
				width: 100%;
				height: 100%;
				display: block;
			}
			
			.itemHref:hover {
				text-decoration: none;
			}
		</style>

	</head>

	<body>
		<div id="wrap">
			<div id="main">
				<div id="header">
					<a id="headImg" href="../colalition.html"><img class="headImg" src="../../../images/12.png" /></a>
					<p id="headTitle">附近</p>
				</div>
				<div id="content">
					<div class="search">
						<input style="width: 100%" id="shopName" type="text" class="search-input" />
						<!--<div class="horizon"></div>-->
						<!--<input style="width: 13%;border-radius: 11px;background:#ed5f69;text-align: center;    line-height: inherit;height: 0.7467rem;padding-left: 0px;color: #fff" type="button" class="search-input" onclick="getTableListData()" value="搜索" />-->			
					</div>
					<div class="items-wrapper" id="items">
					</div>
				</div>
			</div>
			<div id="foot" class="row">
				<a class="foot col-xs-3 col-sm-3" href="../../kaoqin/index2.html">
					<img class="footImg" src="../../../images/15.png" />
					<p>管理</p>
				</a>
				<a class="foot col-xs-3 col-sm-3" href="../colalition.html">
					<img class="footImg" src="../../../images/14.png" />
					<p>联盟</p>
				</a>
				<a class="foot col-xs-3 col-sm-3" href="../../store/store.html">
					<img class="footImg" src="../../../images/11.png" />
					<p>商城</p>
				</a>
				<a class="foot col-xs-3 col-sm-3" href="../../personage/personIndex.html">
					<img class="footImg" src="../../../images/13.png" />
					<p>我的</p>
				</a>
			</div>
		</div>
		<form id="wxForm" name="wxForm" method="post"></form>
	</body>
	
	<script>
		var latitude;
		var cityFn;
			var url = window.location.href;
			
			var tokenzb = $.cookie("tokenRh");
			if(!tokenzb) {
				window.location.href = "../login/login/login.html";
			}
			$().ready(function() {
				currentyf(tokenzb);
				document.onkeydown=keyDownSearch;  
			    function keyDownSearch(e) {
			        // 兼容FF和IE和Opera    
			        var theEvent = e || window.event;    
			        var code = theEvent.keyCode || theEvent.which || theEvent.charCode;    
			        if (code == 13) {    
			            getTableListData();//具体处理函数    
			            return false;    
			        }    
		        return true;    
		    }
		    
			console.log(latitude);
			
		})
		function getTableListData(){
			var url = "http://47.104.13.109/mall-service/shop/list?latitude="+latitude+"&pageNum=1&pageSize=30&shopSpare2="+cityFn;
			var shopName = document.getElementById("shopName").value;   
			if(shopName != null){
				url += "&shopName="+shopName;
			}
			$.ajax({
				url: url,
				type: "GET",
				success: function(data) {
					console.log(data);
					var tpl="";
						$.each(data.list, function(idx, item) {
						var shopId = item.shopId; //商家id；
						tpl+='<div class="item">' +
							'<a class="itemHref" href="../commodity_details/commodity_details.html?shopId=' + shopId + '"><span class="item-title">' + item.shopName + '</span>' +
							'<img class="item-pic" src="' + item.shopPic + '"/>' +
							'<p class="address"><img class="icon" src="../../../images/coalltion/newadress.png">' + "" + '</p>' +
							'<p class="present"><img class="icon" src="../../../images/coalltion/08.png">' + item.shopAdd +
							'</p>' +
							'</a></div>';
						
					})
					document.getElementById('items').innerHTML = tpl;
					
				},
				error: function(e) {
					console.log('获取附近商家失败:', e);
				}
			})
		}
		function getTableListData2(shopName){
			var url = "http://47.104.13.109/mall-service/shop/list?latitude="+latitude+"&pageNum=1&pageSize=30";  
			if(shopName != null){
				url += "&shopSpare2="+shopName;
			}
			//alert(url);
			$.ajax({
				url: url,
				type: "GET",
				success: function(data) {
					console.log(data);
					var tpl="";
						$.each(data.list, function(idx, item) {
						var shopId = item.shopId; //商家id；
						tpl+='<div class="item">' +
							'<a class="itemHref" href="../commodity_details/commodity_details.html?shopId=' + shopId + '"><span class="item-title">' + item.shopName + '</span>' +
							'<img class="item-pic" src="' + item.shopPic + '"/>' +
							'<p class="address"><img class="icon" src="../../../images/coalltion/newadress.png">' + item.shopAdd + '</p>' +
							'<p class="present"><img class="icon" src="../../../images/coalltion/08.png">' + item.shopSign +
							'</p>' +
							'</a></div>';
						
					})
					document.getElementById('items').innerHTML = tpl;
					
				},
				error: function(e) {
					console.log('获取附近商家失败:', e);
				}
			})
		}
		function shouquan(memberId){
			$.get("http://47.104.13.109/pay-service/getOpenId4?memberId="+memberId, {
				rediretUrl:location.href
			}, function(data) {
				//alert(data.url);
				if(data != ""){
					$("#wxForm").prop("action",data.url);
					$("#wxForm").submit();
				}
			},"json");
		}
		function currentyf(tokenzb) {
				$.ajax({
					cache: false,
					type: "GET",
					url: "http://47.104.13.109/member-service/member/mine/member?token=" + tokenzb,
					dataType: "JSON",
					async: false,
					error: function(error) {
						$.removeCookie('tokenzb', {
							expires: 7,
							path: '/'
						});
					},
					success: function(data) {
						console.log(data);
						memberId = data.id;
						var url = window.location.href;
						if(url.indexOf("?") > 0 )
						{
							var url1 = url.split("?")[1];
						    //alert("url1"+url1);
							var url2 = url1.split("&");
							var state = url2[1].split("=")[1];
							$.ajax({
								cache: false,
								type: "GET",
								url: "http://47.104.13.109/pay-service/setOpenId?code="+url2[0].split("=")[1]+"&memberId="+memberId,
								async: false,
								error: function(error) {
								},
								success: function(data) {
									dingwei(memberId,state);
								}
							});
						}else{
							shouquan(memberId);
						}
					}
				});
			}
		function dingwei(memberId,state){
			//alert(state);
			var urlf = window.location.href;
			
			var newUrl = "http://47.104.13.109/pay-service/getJSSDK?memberId=" + memberId + "&url="+urlf;
			//alert(newUrl);
			$.ajax({
				cache: false,
				type: "GET",
				url: newUrl,
				dataType: "JSON",
				async: false,
				success: function(data2) {
					console.log(data2.appId);
					console.log(data2.timestamp);
					console.log(data2.noncestr);
					console.log(data2.signature);
					wx.config({
					   	 debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					    appId: data2.appId, // 必填，公众号的唯一标识
					    timestamp: data2.timestamp, // 必填，生成签名的时间戳
					    nonceStr: data2.noncestr, // 必填，生成签名的随机串
					    signature: data2.signature,// 必填，签名，见附录1
					    jsApiList: ['checkJsApi', 'openLocation', 'getLocation'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
					});
					
					wx.ready(function(){
					    // config信息验证后会执行ready方法，所有接口调用都必须在config接口获得结果之后，config是一个客户端的异步操作，所以如果需要在页面加载时就调用相关接口，则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口，则可以直接调用，不需要放在ready函数中。
						wx.checkJsApi({
							jsApiList: ['getLocation'], // 需要检测的JS接口列表，所有JS接口列表见附录2,  
							success: function(res) {  
								if (res.checkResult.getLocation == false) {    
									alert('你的微信版本太低，不支持微信JS接口，请升级到最新的微信版本！');
							 }  
							}  
						});
						wx.getLocation({ 
							success : function(res) {
								latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90    
								longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。    
								speed = res.speed; // 速度，以米/每秒计    
								accuracy = res.accuracy; // 位置精度    
								//alert("获取到位置信息：latitude="+latitude+"accuracy="+accuracy);
								getBaiduPosition(longitude,latitude);
								
								//alert(accuracy);  
							},    
							cancel : function(res) {    
								//alert('未能获取地理位置，请手动切换城市！');  
							}  
						});
					});
						 
					wx.error(function(res){
				        // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。  
				        //alert("切换城市");  
				        //alert('未能获取地理位置，请手动切换城市！');   
				    });
				}
			});
		}
		
		//百度地图api功能
		function G(id) {
			return document.getElementById(id);
		}
		//坐标转换
		function getBaiduPosition(lng,lat) {
			var url ="http://api.map.baidu.com/geoconv/v1/?coords="+lng+","+lat+"&from=1&to=5&ak=cEywgU0hSEVd31BAlBWPYT331WtoNNdB";
	        $.ajax({
	            url: url,
	            type: 'GET',
	            contentType: "application/json",
	            dataType: 'jsonp',//这里要用jsonp的方式不然会报错
	            success: function(data) {
	            	if(data.status == 0){
		                latitude = data.result[0].x+","+data.result[0].y;
		                
		                var map = new BMap.Map("allmap");
						var point = new BMap.Point(data.result[0].x,data.result[0].y);
						var gc = new BMap.Geolocation();
						var gc2 = new BMap.Geocoder();
				        gc.getCurrentPosition(function (r) {  
				            if (this.getStatus() == BMAP_STATUS_SUCCESS) {  
				                //appLat = r.point.lat;  
				                //appLng = r.point.lng;  
				  
				                var pt = r.point;  
				                gc2.getLocation(pt, function (rs) {  
				                    var addComp = rs.addressComponents;  
				  
				                    var province = addComp.province;  
				                    var city = addComp.city;  
				  					getTableListData2(city);
				  					cityFn = city;
				                });  
				            }  
				        }, { enableHighAccuracy: true });  
						
	            	}
	            }
	        });
	        
    	}
		
	</script>

</html>